package topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import util.Tools;

/**
 * 描述：topic类型的交换机，发送消息
 */

public class EmitLogTopic {
    private static final String EXCHANGE_NAME = "topic_logs";
    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = Tools.getFactory();
        Connection connection = factory.newConnection();
        connection.createChannel();
        // 声明一个direct类型的交换机
        Channel channel = connection.createChannel();
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
        String message = "Animal World!";
        String[] routingKeys = new String[9];
        routingKeys[0] = "quick.orange.rabbit";
        routingKeys[1] = "lazy.orange.elephant";
        routingKeys[2] = "quick.orange.fox";
        routingKeys[3] = "lazy.brown.fox";
        // routingKeys[4]虽然同时符合lazy.#和*.*.rabbit但是只发送一次
        routingKeys[4] = "lazy.pink.rabbit";
        routingKeys[5] = "quick.brown.fox";
        routingKeys[6] = "orange";
        routingKeys[7] = "quick.orange.mail.rabbit";
        routingKeys[8] = "lazy.orange.mail.rabbit";
        for (int i = 0; i < routingKeys.length; i++) {
            channel.basicPublish(EXCHANGE_NAME, routingKeys[i], null, message.getBytes("UTF-8"));
            System.out.println("发送了消息，路由键为：" + routingKeys[i] + "，内容为：" + message);
        }
        // 关闭连接
        channel.close();
        connection.close();
    }
}
